package com.mjh.zhujianju.controller.supervise;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.livew.framework.base.controller.RestBaseController;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.BaseService;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.pojo.LoginUserInfo;
import com.livew.framework.utils.XTCurrentUser;
import com.livew.framework.utils.XTStringUtils;
import com.mjh.zhujianju.model.supervise.SupervisionEO;
import com.mjh.zhujianju.service.supervise.SupervisionService;
import com.mjh.zhujianju.util.ApiConstant;
import com.mjh.zhujianju.util.JsonResult;
import com.mjh.zhujianju.vo.supervision.SupervisionVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author: Alex
 * @version: V1.0
 * @project: zhujianju
 * @package: com.mjh.zhujianju.controller.supervise
 * @description: description
 * @date: 2018/9/5
 * @time: 上午8:46
 */
@RestController
@RequestMapping("/supervise")
@Api("1.督办->待办事项")
public class SupervisionController extends RestBaseController<SupervisionEO> {

	@Autowired
	private SupervisionService supervisionService;

	@Resource
	private FileService fileService;

	@Resource
	private SysUserService userService;

	@ApiOperation(value = "督办->创建督办", notes = "创建督办")
	@ApiImplicitParam(name = "saveInfo", value = "督办实体类", required = true, dataType = "SupervisionVO")
	@RequestMapping(value = "saveInfo", method = RequestMethod.POST)
	public ResponseEntity<JsonResult> saveInfo(@RequestBody SupervisionVO vo) {
		JsonResult result = new JsonResult();
		if (vo == null || XTStringUtils.isEmpty(vo.getTitle())) {
			result.setResult("参数错误");
			result.setStatus(ApiConstant.MSG_ERROR);
			return ResponseEntity.ok(result);
		}
		try {
			vo.setUserId(XTCurrentUser.getCurrentUserId());
			// vo.setType(SupervisionTypeEnum.internal.getCode());
			supervisionService.saveInfo(vo);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			result.setResult(e.getMessage());
			result.setStatus(ApiConstant.MSG_ERROR);
			return ResponseEntity.ok(result);
		}
		result.setResult("保存成功");
		result.setStatus(ApiConstant.MSG_SUCCESS);
		return ResponseEntity.ok(result);
	}

	@RequestMapping(value = "/spervisionListFromMe", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> spervisionListFromMe() {
		JsonResult result = new JsonResult();
		LoginUserInfo info = XTCurrentUser.getCurrentUser();
		List<SupervisionVO> supervisionVOS = supervisionService.getSupervisionListFromMe(info.getId());
		result.setResult(supervisionVOS);
		result.setStatus(ApiConstant.MSG_SUCCESS);
		return ResponseEntity.ok(result);
	}

	@RequestMapping(value = "/spervisionListToMe", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> spervisionListToMe() {
		JsonResult result = new JsonResult();
		LoginUserInfo info = XTCurrentUser.getCurrentUser();
		List<SupervisionVO> supervisionVOS = supervisionService.getSupervisionListToMe(info.getId());
		result.setResult(supervisionVOS);
		result.setStatus(ApiConstant.MSG_SUCCESS);
		return ResponseEntity.ok(result);
	}

	@ApiOperation(value = "督办->督办办理", notes = "根据督办主键和用户微信id查询督办详情")
	@RequestMapping(value = "/letterEvaluateDetail/{id}", method = RequestMethod.GET)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "督办主键", required = true, dataType = "Long", paramType = "path"),
			@ApiImplicitParam(name = "openid", value = "微信用户id", required = true, dataType = "String", paramType = "path") })
	public ResponseEntity<JsonResult> letterEvaluateDetail(@PathVariable(value = "id") Long id) {
		JsonResult result = new JsonResult();
		LoginUserInfo info = XTCurrentUser.getCurrentUser();
		SupervisionVO supervisionVO = null;
		try {
			supervisionVO = supervisionService.querySupervisionScheduleDetail(id, info.getId());
		} catch (XTBusinessException e) {
			result.setResult(e.getMessage());
			result.setStatus(ApiConstant.MSG_ERROR);
			return ResponseEntity.ok(result);
		}
		result.setStatus(ApiConstant.MSG_SUCCESS);
		result.setResult(supervisionVO);
		return ResponseEntity.ok(result);
	}

	/**
	 * 附件
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/fuJian/{id}", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> fuJian(@PathVariable(value = "id") Long id) {
		JsonResult result = new JsonResult();
		List<FileEO> files = fileService.getFileById(id, SupervisionEO.class.getName(), null);
		result.setResult(files);
		result.setStatus(ApiConstant.MSG_SUCCESS);
		return ResponseEntity.ok(result);
	}

	@ApiOperation(value = "督办->督办办理", notes = "根据督办主键和用户微信id查询督办详情")
	@RequestMapping(value = "/getInfo/{id}", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> getInfo(@PathVariable(value = "id") Long id) {
		JsonResult result = new JsonResult();
		SupervisionVO supervisionVO = null;
		try {
			supervisionVO = supervisionService.getAPIInfo(id);
		} catch (XTBusinessException e) {
			result.setResult(e.getMessage());
			result.setStatus(ApiConstant.MSG_ERROR);
			return ResponseEntity.ok(result);
		}
		result.setStatus(ApiConstant.MSG_SUCCESS);
		result.setResult(supervisionVO);
		return ResponseEntity.ok(result);
	}

	/**
	 * 
	 * @Description: 更新
	 * @param vo
	 * @return
	 * @author wt1293
	 * @date 2018年8月21日下午1:46:14
	 *
	 */

	@RequestMapping(value = "updateInfo", method = RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<JsonResult> updateInfo(@RequestBody SupervisionVO vo) {
		JsonResult result = new JsonResult();
		if (vo == null) {
			result.setResult("参数错误");
			result.setStatus(ApiConstant.MSG_ERROR);
			return ResponseEntity.ok(result);
		}
		try {
			supervisionService.updateInfo(vo);
		} catch (XTBusinessException e) {
			e.printStackTrace();
			result.setResult(e.getMessage());
			result.setStatus(ApiConstant.MSG_ERROR);
			return ResponseEntity.ok(result);
		}
		result.setStatus(ApiConstant.MSG_SUCCESS);
		result.setResult("保存成功");
		return ResponseEntity.ok(result);
	}

	@Override
	protected BaseService<SupervisionEO> getService() {
		return supervisionService;
	}
}
